﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class AddAdvisor : Form
    {
        public AddAdvisor()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            Advisor advisor = new Advisor();

            advisor.Code = textBoxAdvisorCode.Text;
            advisor.Name = textBoxAdvisorName.Text;
            advisor.Title = textBoxTitle.Text;
            advisor.Initials = textBoxInitials.Text;
            advisor.Surname = textBoSurname.Text;
            advisor.Address_1 = textBoxAddress1.Text;
            advisor.Address_2 = textBoxAddress2.Text;
            advisor.Telephone1 = textBoxTelephone.Text;
            advisor.Fax = textBoxFax.Text;
            advisor.CreatedDate = DateTime.Today;

            try
            {
                AdvisorServices.Add(advisor);
            }
            catch (System.Exception ex)
            {
                if (ex.Message.StartsWith("ORA-00001: unique"))
                {
                    MessageBox.Show("Advisor " + textBoxAdvisorCode.Text + " already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Failed to create new advisor\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return;
            }
            MessageBox.Show("Advisor added successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (checkBoxDontClose.Checked == false)
            {
                Close();
            }
            else
            {
                textBoxAdvisorCode.Text = "";
                textBoxAdvisorName.Text = "";
                textBoxTitle.Text = "";
                textBoxInitials.Text = "";
                textBoSurname.Text = "";
                textBoxAddress1.Text = "";
                textBoxAddress2.Text = "";
                textBoxTelephone.Text = "";
                textBoxFax.Text = "";
                textBoxAdvisorCode.Focus();
            }
        }

        private bool validateInputs()
        {
            if (textBoxAdvisorCode.Text == "")
            {
                MessageBox.Show("Enter advisor code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AddAdvisor_Load(object sender, EventArgs e)
        {

        }

        private void buttonCancel_Click_1(object sender, EventArgs e)
        {
            Close();
        }
    }
}
